package com.jsbj.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsbj.tools.DateUtils;
import com.jsbj.tools.RequestHeaderUtils;
import com.jsbj.user.entities.SysLoginRecord;
import com.jsbj.user.entities.SysUser;
import com.jsbj.user.mapper.SysLoginRecordMapper;
import com.jsbj.user.service.ISysLoginRecordService;
import com.jsbj.user.service.ISysUserService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 登录记录表 服务实现类
 * </p>
 *
 * @author jsbj
 * @since 2022-03-14
 */
@Service
public class SysLoginRecordServiceImpl extends ServiceImpl<SysLoginRecordMapper, SysLoginRecord> implements ISysLoginRecordService {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public void saveLoginRecord(Map<String, Object> userInfo) {
        SysLoginRecord sysLoginRecord = new SysLoginRecord();
        sysLoginRecord.setUserId(MapUtils.getString(userInfo, "uid"));
        // 查询登录信息
        SysUser sysUser = sysUserService.getById(MapUtils.getString(userInfo, "uid"));
        if (null != sysUser) {
            sysLoginRecord.setUserName(sysUser.getUserName());
            sysLoginRecord.setLoginId(sysUser.getLoginId());
        }
        Random rand=new Random();
        int n=rand.nextInt(30)+30;
        sysLoginRecord.setRemark(n+"");

        sysLoginRecord.setMessage("PC登录成功");
        sysLoginRecord.setLoginTime(new Date());
        baseMapper.insert(sysLoginRecord);
    }

    @Override
    public List<SysLoginRecord> getLoginRecordByPhoneNum(List<String> phoneNums) {
        LambdaQueryWrapper<SysLoginRecord> qw = new LambdaQueryWrapper<>();
        qw.in(SysLoginRecord::getLoginId, phoneNums);
        qw.between(SysLoginRecord::getLoginTime, DateUtils.dateTime(new Date()) + " 00:00:00", DateUtils.dateTime(new Date()) + " 23:59:59");
        return baseMapper.selectList(qw);
    }

    @Override
    public Map<String, Object> recentVisits() {
        String tenantCode = RequestHeaderUtils.getTenantCode(request);
        Map<String, Object> ret = new HashMap<>();
        // 查询配有当前项目的角色对应的人员的登录记录
        List<SysLoginRecord> sysLoginRecords = baseMapper.recentVisits(tenantCode);
        for (SysLoginRecord sysLoginRecord : sysLoginRecords) {
            sysLoginRecord.setLoginTimeStr(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, sysLoginRecord.getLoginTime()));
        }
        ret.put("list", sysLoginRecords);
        ret.put("title", "最近访问");
        return ret;
    }

    @Override
    public Map<String, Object> useCase() {
        String tenantCode = RequestHeaderUtils.getTenantCode(request);
        Map<String, Object> ret = new HashMap<>();
        // 总登录次数
        Integer zdlcs = baseMapper.selectzdlcs(tenantCode);
        if (null == zdlcs) {
            zdlcs = 0;
        }
        // 总登录人数
        Integer zdlrs = baseMapper.selectzdlrs(tenantCode);
        if (null == zdlrs) {
            zdlrs = 0;
        }
        // 总登录时长
        Integer dlsc = baseMapper.selectdlsc(tenantCode);
        if (null == dlsc) {
            dlsc = 0;
        }

        // 各标段近七日登录统计
        List<Map<String, Objects>> list = baseMapper.selectSevenDay(tenantCode);

        ret.put("list", list);
        ret.put("zdlcs", zdlcs);
        ret.put("zdlrs", zdlrs);
        ret.put("dlsc", dlsc);
        ret.put("title", "使用情况");
        return ret;
    }
}
